package cn.toma.core.result;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.List;

/**
 * @author zqx
 * @version 1.0.0
 * @ClassName PageTool.java
 * @Description
 * @createTime 2019年08月29日 14:42:00
 */
public class PageTool implements Serializable
{

	/**
	 * 页码
	 */
	private int pageSize;
	/**
	 * 页面大小
	 */
	private int pageNum;
	/**
	 * 总页数
	 */
	private long total;
	/**
	 * 总记录数
	 */
	private int pages;
	/**
	 * 是否为第一页
	 */

	private boolean isFirstPage;
	/**
	 * 是否为最后一页
	 */
	private boolean isLastPage;
	/**
	 * 数据
	 */
	private List<?> list;

	public PageTool()
	{
	}

	public PageTool(int pageNo, int pageSize)
	{
		this.pageNum = pageNo;
		this.pageSize = pageSize;
	}

	/**
	 * 初始化页码
	 *
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public static PageTool init(int pageNum, int pageSize)
	{
		if (pageNum <= 0 || pageSize <= 0) {
			pageNum = 1;
			pageSize = 10;
		}
		pageNum = (pageNum - 1) * pageSize;
		PageTool pageTool = new PageTool(pageNum, pageSize);
		return pageTool;
	}

	public PageTool(int pageSize, int pageNum, int total, int pages, boolean isLastPage, boolean isFirstPage, List<?> list)
	{
		this.pageSize = pageSize;
		this.pageNum = pageNum;
		this.total = total;
		this.pages = pages;
		this.isLastPage = isLastPage;
		this.isFirstPage = isFirstPage;
		this.list = list;
	}

	/**
	 * api 手动分页工具类
	 *
	 * @param pageTool
	 * @param count
	 * @param list
	 * @return
	 */
	public static PageTool pageTool(PageTool pageTool, Integer count, List<?> list)
	{
		Integer pageNo = pageTool.getPageNum();
		Integer pageSize = pageTool.getPageSize();
		Integer tp = count / pageSize;
		if (count % pageSize > 0) {
			tp = count / pageSize + 1;
		}
		if (pageNo == 0) {
			pageNo = pageNo + 1;
		}
		else {
			pageNo = (pageNo / pageSize) + 1;
		}
		boolean isFirstPage;
		if (pageNo == 1) {
			isFirstPage = true;
		}
		else {
			isFirstPage = false;
		}
		boolean isLastPage;
		if (pageNo.equals(tp) || tp == 0) {
			isLastPage = true;
		}
		else {
			isLastPage = false;
		}
		return new PageTool(pageSize, pageNo, count, tp, isLastPage, isFirstPage, list);
	}


	public static PageTool pageTool(PageInfo<?> pageInfo)
	{
		PageTool pageTool = new PageTool();
		BeanUtils.copyProperties(pageInfo, pageTool);
		pageTool.setFirstPage(pageInfo.isIsFirstPage());
		pageTool.setLastPage(pageInfo.isIsLastPage());
		return pageTool;
	}


	public int getPageSize()
	{
		return pageSize;
	}

	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}

	public int getPageNum()
	{
		return pageNum;
	}

	public void setPageNum(int pageNum)
	{
		this.pageNum = pageNum;
	}

	public long getTotal()
	{
		return total;
	}

	public void setTotal(long total)
	{
		this.total = total;
	}

	public int getPages()
	{
		return pages;
	}

	public void setPages(int pages)
	{
		this.pages = pages;
	}

	public boolean isFirstPage()
	{
		return isFirstPage;
	}

	public void setFirstPage(boolean firstPage)
	{
		isFirstPage = firstPage;
	}

	public boolean isLastPage()
	{
		return isLastPage;
	}

	public void setLastPage(boolean lastPage)
	{
		isLastPage = lastPage;
	}

	public List<?> getList()
	{
		return list;
	}

	public void setList(List<?> list)
	{
		this.list = list;
	}
}
